package com.tg.web.backend;

import com.tg.model.EbProductCategory;
import com.tg.service.ProductCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/productcategory")
public class ProductCategoryController {
    @Autowired
    private ProductCategoryService productCategoryService;

    @RequestMapping("/to_save")
    public String toSave(Model model){
        Map<String, List<EbProductCategory>> categoryMap = productCategoryService.findBigCategory();
        model.addAttribute("categoryMap",categoryMap);
        return "manage/productClass-add";
    }


    @RequestMapping("/save")
    public String save(EbProductCategory ebProductCategory){
        System.out.println("ebProductCategory==>"+ebProductCategory);
        int result=productCategoryService.save(ebProductCategory);
        if (result<1){return  "manage/manage-error";}
        return "manage/manage-result";
    }

//    @RequestMapping("/select")
//    public String select(Model model){
//        Map<String, List<EbProductCategory>> categoryMap = productCategoryService.findAllCategory();
//        //2.通过model传递给jsp
//        model.addAttribute("categoryMap",categoryMap);
//        return "manage/productClass";
//    }

    @RequestMapping("/select")
    public String select(Model model){
//        System.out.println("1111111");
        //查询所有大类和小类
        //1.调用商品分类的service层 查询大类和小类
        Map<String, List<EbProductCategory>> categoryMap = productCategoryService.findBigAndSmallCategory();
        //2.通过model传递给jsp
//        System.out.println("categoryMap===>"+categoryMap);
        model.addAttribute("categoryMap",categoryMap);

        return "manage/productClass";
    }

    @RequestMapping("/to_update")
    public String to_updete(Model model,Integer epcId){
        System.out.println("epcId==>"+epcId);
        EbProductCategory ebProductCategory=productCategoryService.selectOne(epcId);
        model.addAttribute("ebProductCategory",ebProductCategory);
        Map<String,List<EbProductCategory>> categoryMap=productCategoryService.findBigCategory();
        model.addAttribute("categoryMap",categoryMap);

        return  "manage/productClass-modify";
    }

    @RequestMapping("/update")
    public String edit(EbProductCategory ebProductCategory){
        System.out.println(ebProductCategory);
        //1.调用service更新商品的方法
        int result = productCategoryService.updateProductCategory(ebProductCategory);
        //2.根据添加的结果(result)跳转页面
        if(result<=0){
            return "manage/manage-error";
        }
        return "manage/manage-result";
    }

        @RequestMapping("/delete")
    public String delete(Model model,Integer epcId){
            System.out.println("epcId==>"+epcId);
        int result=productCategoryService.deleteProductCategory(epcId);
            if(result<=0){
                return "manage/manage-error";
            }
            return "manage/manage-result";
    }
}

//    @RequestMapping("/to_update")
//    public String edit(EbProductCategory ebProductCategory,
//                       HttpServletRequest request){
//        System.out.println(ebProductCategory);
//
//        //1.调用service更新商品的方法
//        int result = productCategoryService.selectOne(ebProductCategory);
//        //2.根据添加的结果(result)跳转页面
//        if(result<=0){
//            return "null";
//        }
//        return "manage/manage-result";
//    }

//    @RequestMapping
//    public int update(EbProductCategory ebProductCategory) {
//        //1.获取类的id
//        int bigId = ebProductCategory.getEpcId();
//        ebProductCategory.setEpcId(bigId);
//        return EbProductCategoryMapper.updateByPrimaryKey1(ebProductCategory);
//    }


